// import {
// 	getUserInfo
// } from "../../api/user.js";
import {
	LOGIN_STATUS,
	UID,
	USERPHOTO,
	SERVICE_UID,
	DOCTOR_UID,
	PLATFORM,
	TEMP,
	CART_INFO,
	STORE_TYPE,
	STORE_ID,
	Flag,
	STORE_NAME,
	PATIENT_ID,
	CATEGORY_ID,
	DRUG_DETAIL,
	pointFlag
} from '../../config/cache';
import Cache from '../../utils/cache';
import {
	USER_INFO
} from '../../config/cache';

const state = {
	token: Cache.get(LOGIN_STATUS) || '',
	// backgroundColor: "#fff",Temp
	// userInfo: Cache.get(USER_INFO)?JSON.parse(Cache.get(USER_INFO)):null,
	cartInfo: Cache.get(CART_INFO) ? JSON.parse(Cache.get(CART_INFO)) : null,
	uid: Cache.get(UID) || null,
	doctorUid: Cache.get(DOCTOR_UID) || null,
	serviceUid: Cache.get(SERVICE_UID) || null, //服务人员uid
	userPhoto: Cache.get(USERPHOTO) || null,
	temp: Cache.get(TEMP) || null,
	storeType: Cache.get(STORE_TYPE) || null,
	storeId: Cache.get(STORE_ID) || null,
	flag: Cache.get(Flag) || null,
	storeName: Cache.get(STORE_NAME) || null,
	patientId: Cache.get(PATIENT_ID) || null,
	categoryId: Cache.get(CATEGORY_ID) || null,
	drugDetail: Cache.get(DRUG_DETAIL) || null,
	pointFlag: Cache.get(pointFlag) || null,
	// homeActive: false,
	// chatUrl: Cache.get('chatUrl') || '',
	// systemPlatform: Cache.get(PLATFORM)?Cache.get(PLATFORM):'',
	// productType: Cache.get('productType') || ''
	action: 0
};

const mutations = {
	NEXT(state, val) {
		state.action = val;;

	},
	SET_TEMP(state, val) {
		state.temp = val;
		Cache.set(TEMP, val)
	},
	SET_USER_PHOTO(state, val) {
		state.userPhoto = val;
		Cache.set(USERPHOTO, val)
	},
	LOGIN(state, opt) {
		state.token = opt.token;
		Cache.set(LOGIN_STATUS, opt.token);
		//重新登录清除绑定服务人员、缓存
		// Cache.clear(DOCTOR_UID); ///医生
		Cache.clear(SERVICE_UID)

	},
	SETUID(state, val) {
		state.uid = val;
		Cache.set(UID, val);
	},
	SET_DOCTOR_UID(state, val) {
		state.doctorUid = val;
		Cache.set(DOCTOR_UID, val);
	},
	SET_SERVICE_UID(state, val) {
		state.serviceUid = val;
		Cache.set(SERVICE_UID, val);
	},
	SET_PATIENT_ID(state, val) {
		state.patientId = val;
		Cache.set(PATIENT_ID, val);
	},
	SET_CATEGORY_ID(state, val) {
		state.categoryId = val;
		Cache.set(CATEGORY_ID, val)
	},

	SET_DRUG_DETAIL(state, val) {
		state.drugDetail = val;
		Cache.set(DRUG_DETAIL, val)
	},

	SET_STORETYPE(state, val) {
		state.storeType = val;
		Cache.set(STORE_TYPE, val);
	},
	SET_STOREID(state, val) {
		state.storeId = val;
		Cache.set(STORE_ID, val);
	},
	SET_Flag(state, val) {
		state.flag = val;
		Cache.set(Flag, val);
	},
	SET_STORENAME(state, val) {
		state.storeName = val;
		Cache.set(STORE_NAME, val);
	},
	UPDATE_LOGIN(state, token) {
		state.token = token;
	},
	LOGOUT(state) {
		state.token = undefined;
		state.uid = undefined
		state.pointFlag = undefined
		Cache.clear(LOGIN_STATUS);
		Cache.clear(UID);
		Cache.clear(USER_INFO);
		Cache.clear(pointFlag);
	},
	BACKGROUND_COLOR(state, color) {
		state.color = color;
		document.body.style.backgroundColor = color;
	},
	UPDATE_USERINFO(state, userInfo) {
		state.userInfo = userInfo;
		Cache.set(USER_INFO, userInfo);
	},
	UPDATE_CARTINFO(state, cartInfo) {
		state.cartInfo = cartInfo;
		Cache.set(CART_INFO, cartInfo);
	},
	CLEAR_CARTINFO(state) {
		state.cartInfo = undefined;
		Cache.clear(CART_INFO);
	},
	ADD_CARTINFO(state, ) {
		state.cartInfo = state.cartInfo.push(cartInfoAdd);
		Cache.set(CART_INFO, cartInfo);
	},
	OPEN_HOME(state) {
		state.homeActive = true;
	},
	CLOSE_HOME(state) {
		state.homeActive = false;
	},
	SET_CHATURL(state, chatUrl) {
		state.chatUrl = chatUrl;
	},
	// AuthorizeType(state, authorizeType){
	// 	state.authorizeType = authorizeType;
	// },
	SYSTEM_PLATFORM(state, systemPlatform) {
		state.systemPlatform = systemPlatform;
		Cache.set(PLATFORM, systemPlatform);
	},
	//更新useInfo数据
	changInfo(state, payload) {
		state.userInfo[payload.amount1] = payload.amount2;
		Cache.set(USER_INFO, state.userInfo);
	},
	//商品类型，用于区分视频号商品与一般商品
	PRODUCT_TYPE(state, productType) {
		state.productType = productType;
		Cache.set('productType', productType);
	},
	// 新增pointGiveawayFlag字段 为1弹窗提示30积分已发放
	pointFlag(state, flag) {
		state.pointFlag = flag;
		Cache.set('pointFlag', flag);
	}

};

const actions = {
	// setActive({
	// 	state,
	// })



	// USERINFO({
	// 	state,
	// 	commit
	// }, force) {
	// 	return new Promise(reslove => {
	// 		getUserInfo().then(res => {
	// 			commit("UPDATE_USERINFO", res.data);
	// 			reslove(res.data);
	// 		});
	// 	}).catch(() => {

	// 	});
	// 	// debugger
	// 	// if (state.userInfo !== null && !force)
	// 	// 	return Promise.resolve(state.userInfo);
	// 	// else
	// 	// 	return new Promise(reslove => {
	// 	// 		getUserInfo().then(res => {
	// 	// 			commit("UPDATE_USERINFO", res.data);
	// 	// 			reslove(res.data);
	// 	// 		});
	// 	// 	}).catch(() => {

	// 	// 	});
	// }


};

export default {
	state,
	mutations,
	actions
};